import {getStudentInfo} from '../../api/studentApi.js';
import {changePassword} from "../../api/loginApi";
import {changeStudentInfo} from '../../api/studentApi.js';
import {getRecordsList} from '../../api/studentApi.js';
import {deleteRecord} from '../../api/studentApi.js';
import {getQuestion} from '../../api/studentApi.js';
import {getClass, getCollage, getMajor} from "../../api/studentApi";
import Cookies from "js-cookie";

const state = {
    student_info:Cookies.get('student_info')===undefined?null:JSON.parse(Cookies.get('student_info')),
    question_list:[],
    question_list_total_page:1,
    collage_list:[],
    major_list:[],
    class_list:[],
    questData:[],
}

const mutations = {
    SET_STUDENT_INFO: (state, student_info)=>{
        state.student_info = student_info;
        Cookies.set('student_info', student_info);
    },
    SET_QUESTION_LIST: (state, question_list)=>{
        state.question_list = question_list;
        question_list.forEach((item,index) =>{
            item.key = item.queRecId;
            item.qurRecNum = state.questData.offset+index+1;
        })
    },
    SET_QUESTION_LIST_TOTAL_PAGE: (state, question_list_total_page)=>{
        state.question_list_total_page = question_list_total_page;
    },
    SET_COLLAGE:(state,collage_list)=>{
        state.collage_list=[]
        collage_list.forEach(item=>{
            state.collage_list.unshift({
                value:item.colId,
                label:item.name,
            })
        });

    },
    SET_COLLAGE_MAJOR:(state, data)=>{
        const {major_list} = data;
        state.major_list=[]
        major_list.forEach(maj =>{
            state.major_list.unshift({
                value:maj.majorId,
                label:maj.name,
            })
        })
    },
    SET_COLLAGE_MAJOR_CLASS:(state, data)=>{
        const {class_list} = data;
        state.class_list=[];
        class_list.forEach(cla =>{
            state.class_list.unshift({
                value:cla.claId,
                label: cla.claNumber,
            })
        })

    },
    NULL:()=> {}
}

const actions = {

    getStudentInfo({commit}, data){ //获取学生信息
        const {token, studentID} = data;
        return new Promise((resolve, reject) => {
            getStudentInfo({studentID:studentID,token:token}).then(res=>{
                if (res.code === 1) {
                    commit('SET_STUDENT_INFO', res.data);
                }
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },

    changePassword({ commit },data){ //修改密码
        const {token, studentID, password, newPassword} = data;
        return new Promise((resolve, reject) => {
            changePassword({token, studentID, password, newPassword}).then(res=>{
                commit('NULL');
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },

    changeStudentInfo({ commit },data){ //修改学生信息
        return new Promise((resolve, reject) => {
            const {studentInfo,token,studentID} = data;
            changeStudentInfo({studentInfo,token,studentID}).then(res=>{
                if(res.code === 1){
                    actions.getStudentInfo({commit},{token, studentID:studentID}).then(
                        data=>{
                            if(data !==null)
                                resolve(res);
                        }
                    );
                }
            }).catch(error => {
                reject(error)
            })
        })
    },

    getRecordsList({commit,state},data){ //获取记录
        state.questData = data;
        return new Promise((resolve, reject) => {
            getRecordsList(data).then(
                res => {
                    if (res.code === 1) {
                        commit('SET_QUESTION_LIST_TOTAL_PAGE',res.data.totalPage);
                        commit('SET_QUESTION_LIST',res.data.recordList);
                    }
                    resolve(res);
                }
            ).catch(error => {
                reject(error)
            })
        })
    },

    deleteRecord({commit},data){ // 删除记录
        const {token,recordIDList,getNewRecordInfo} = data;
        return new Promise((resolve, reject) => {
            deleteRecord({token,recordIDList}).then(
                res => {
                    if (res.code === 1) {
                        actions.getRecordsList({commit,state},getNewRecordInfo).then(getRecordres=>{
                            if(getRecordres.code===1){
                                resolve(getRecordres);
                            }
                        })
                    }
                }
            ).catch(error => {
                reject(error)
            })
        })
    },

    getQuestion({commit},quenID){
        commit('NULL');
        return new Promise((resolve, reject) => {
            getQuestion({questionID:quenID}).then(
                res => {
                    resolve(res);
                }
            ).catch(error => {
                reject(error)
            })
        })
    },

    getCollage({commit}) {
        return new Promise((resolve, reject) => {
            getCollage().then(res => {
                if (res.code === 1) {
                    commit('SET_COLLAGE', res.data);
                }else {
                    this.$message.error('网络异常')
                }
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },
    getMajor({commit},col_id) {
        return new Promise((resolve, reject) => {
            getMajor({collageID:col_id}).then(res => {
                const data = {
                    colId: col_id,
                    major_list: res.data,
                }
                if (res.code === 1) {
                    commit('SET_COLLAGE_MAJOR', data);

                }else {
                    resolve(res);
                }
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },
    getClass({commit},maj_id){
        return new Promise((resolve,reject)=>{
            // console.log('100')
            getClass({majorID:maj_id}).then(res =>{
                const data ={
                    majorId:maj_id,
                    class_list:res.data,
                }
                if (res.code === 1){
                    commit('SET_COLLAGE_MAJOR_CLASS',data);
                }else {
                    resolve(res);
                }
                resolve(res);
            }).catch(error =>{
                reject(error)
            })
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
